package com.tl.commonlibrary.network;

import com.google.gson.Gson;
import com.tl.commonlibrary.AppConfig;
import com.tl.commonlibrary.network.bean.base.Data;
import com.tl.commonlibrary.storage.configuration.PrefUtil;
import com.tl.commonlibrary.ui.beans.User;
import com.tl.commonlibrary.ui.user.Account;

import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/7/10 0010.
 */

public class NetConfig {
    public static final String KEY_DUS = "key_dus";
    public static String API_URL = null;
    public static String API_URL_H5 = null;
    public static final String API_PICTURE = "http://www.cn2401.com/";
    public static final String URL_QQ_SERVICE = "http://q.url.cn/s/92skP4m?_type=wpa";
    public static final String URL_QQ = "mqqwpa://im/chat?chat_type=wpa&uin=%1$s";

    static {
        releaseURL();
    }

    public static final String DATA = "data";

    public static final String KEY_USER_ID = "memberId";
    public static final String KEY_PAGE_SIZE = "pageSize";
    public static final String KEY_PAGE_NUM = "pageNum";
    public static final String KEY_ORDER_ID = "orderId";

    public static final int PAGE_SIZE = 10;

    public static final void init(boolean debuggable) {
        if (debuggable) {
            if (AppConfig.appContext != null) {
                PrefUtil prefUtil = new PrefUtil(AppConfig.appContext);
                String url = prefUtil.getString(KEY_DUS, "debug");
                if ("release".equals(url)) {
                    releaseURL();
                } else if ("debug".equals(url)) {
                    debugURL();
                } else {
                    customURL(url);
                }
            } else {
                releaseURL();
            }
        } else {
            releaseURL();
        }
    }

    private static void releaseURL() {
        API_URL = "https://api.exam.tophrmimg.com/";//正式环境
        API_URL_H5 = API_URL + "h5/";//正式环境
    }

    private static void debugURL() {
        //http://ceshi.cn2401.com:8086/cn2401/";
        API_URL = "https://api.exam.tophrmimg.com/";//测试环境
        API_URL_H5 = API_URL + "h5/";//测试环境
    }

    private static void customURL(String url) {
        API_URL = url;//自定义环境
        API_URL_H5 = API_URL + "h5/";//自定义环境
    }

//    public static RequestBody getBody() {
//        return getBody(null);
//    }
//
//    public static RequestBody getBody(HashMap<String, Object> map) {
//        return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), getDataParam(map));
//    }
    public static String getDataParam(HashMap<String, Object> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        Gson gson = new Gson();
        return gson.toJson(map);
    }
//    public static String getDataParam() {
//        return getDataParam(null);
//    }

//    public static String getDataParam(HashMap<String, String> map) {
//        return getDataParam(Account.getUser(), map);
//    }
//
//    public static String getDataParam(User user, HashMap<String, String> map) {
//        Gson gson = new Gson();
//        Data data = new Data(user);
//        if (map == null) {
//            map = new HashMap<>();
//        }
//        if (!map.containsKey(NetConfig.KEY_USER_ID)) {
//            if (user != null) {
//                map.put(NetConfig.KEY_USER_ID, String.valueOf(user.getUserId()));
//            }
//        }
//        data.body = gson.toJsonTree(map);
//        return gson.toJson(data);
//    }
}
